clear all

cd "_______________"

// This do file produces the LASSO results in Section 4 as well as the relevant supplementary results

log using "log_LASSO_results.log", replace
use "PM10_locoff_befandaft_merge.dta", clear
drop temp

by id year, sort: gen temp=_n 
drop if temp>1
drop temp


xtset id year 

rename posranking posrk
qui tab posrk, gen(posrk)

// define secretary variables
gen match_pr=(posprovince==birthprovince)
gen colgnb=(edu_ft_entertime>=197100 & edu_ft_entertime<197800)
gen col78=(edu_ft_entertime>=197800 & edu_ft_entertime<197900) // first two cohorts after wenge
gen age=year-floor(birthday/100)
gen yearstore=57-age  //year left to be possibly promoted to the next step..
replace yearstore=62-age if posrk==5 
replace yearstore=67-age if posrk==6 
replace yearstore=0 if yearstore<0
gen yeartoredum1=(yearstore==0)
gen yeartoredum2=(yearstore<=5&yearstore>0 )
gen yeartoredum3=(yearstore>5 )
gen ft_col=(edu_ft_highest==3|edu_ft_highest==4)
gen ft_stem=(edu_ft_major==2 | edu_ft_major==3)
gen pt1=(edu_pt_highest<=3& edu_pt_highest!=.)
gen pt2=(edu_pt_highest>=4& edu_pt_highest!=.)
gen  ft_key=(edu_ft_key1==1|edu_ft_key2==1)
gen yearsinpos=year-floor(posstart/100)
rename befcitypartysecretary befcitysec
rename befcountypartysecretary befctysec
rename befcountymayor befctymayor
rename befotherprovince befotherprov
rename befyearsinprovince befyearsinpr
replace befyearsinpr=1*(befyearsinpr>0)
replace befcentralgov=1*(befcentralgov>0)
replace befyearsincity=1*(befyearsincity>0)
replace befotherprov=1*(befotherprov>0)

// define mayor variables
gen m_match_pr=(mayor_posprovince==mayor_birthprovince)
gen m_colgnb=(mayor_edu_ft_entertime>=197100 & mayor_edu_ft_entertime<197800)
gen m_col78=(mayor_edu_ft_entertime>=197800 & mayor_edu_ft_entertime<197900) // first two cohorts after wenge
gen m_age=year-floor(mayor_birthday/100)
gen m_yearstore=57-m_age  //year left to be possibly promoted to the next step..
replace m_yearstore=62-m_age if posrk==5 
replace m_yearstore=67-m_age if posrk==6
replace m_yearstore=0 if m_yearstore<0
gen m_yeartoredum1=(m_yearstore==0)
gen m_yeartoredum2=(m_yearstore<=5&m_yearstore>0 )
gen m_yeartoredum3=(m_yearstore>5 )
gen m_ft_col=(mayor_edu_ft_highest==3|mayor_edu_ft_highest==4)
gen m_ft_stem=(mayor_edu_ft_major=="2"| mayor_edu_ft_major=="3")
gen m_pt1=(mayor_edu_pt_highest<=3& mayor_edu_pt_highest!=.)
gen m_pt2=(mayor_edu_pt_highest>=4& mayor_edu_pt_highest!=.)
gen  m_ft_key=(mayor_edu_ft_key1==1|mayor_edu_ft_key2==1)
replace  m_ft_key=0 if  m_ft_key==.
gen m_yearsinpos=year-floor(mayor_posstart/100)
rename mayor_posprovcpc m_posprovcpc
rename mayor_befyearsinprovince m_befyearsinpr
replace m_befyearsinpr=(m_befyearsinpr>0)
replace mayor_befcentralgov=(mayor_befcentralgov>0)
rename mayor_male m_male
rename mayor_han m_han
rename mayor_befcitypartysecretary m_befcitysec
rename mayor_befcitymayor m_befcitymayor
rename mayor_befsecretary  m_befsecretary
rename mayor_befenterprise m_befenterprise 
rename mayor_befresearch m_befresearch 
rename mayor_befcentralgov m_befcentralgov 
rename mayor_befyearsincity m_befyearsincity
replace m_befyearsincity=(m_befyearsincity>0)
rename mayor_befcountypartysecretary m_befctysec
rename mayor_befcountymayor m_befctymayor
rename mayor_befotherprovince m_befotherprov
replace m_befotherprov=(m_befotherprov>0)
qui by id name, sort: gen sec_t=_n
qui by id mayor_name, sort: gen mayor_t=_n


// define mutually exclusive demographic variables
gen malehan=male*han
gen femalehan=(1-male)*han
gen malenonhan=male*(1-han)
gen m_malehan=m_male*m_han
gen m_femalehan=(1-m_male)*m_han
gen m_malenonhan=m_male*(1-m_han)

save "PM10_locoff_befandaft_merge_collapse.dta", replace

global ctr_sec_demo="malehan femalehan malenonhan" 
global ctr_sec_poly="yearsinpos yearstore match_pr befenterprise befresearch befsecretary befctymayor befctysec befcitymayor befcitysec befcentralgov befyearsincity befyearsinpr befotherprov"

global ctr_sec_edu="ft_col ft_stem ft_key colgnb col78 pt1 pt2"

global ctr_mayor_demo="m_malehan m_femalehan m_malenonhan" 
global ctr_mayor_poly="m_yearsinpos m_yearstore m_match_pr m_befenterprise m_befresearch m_befsecretary m_befctymayor m_befctysec m_befcitymayor m_befcitysec m_befcentralgov m_befyearsincity m_befyearsinpr m_befotherprov"

global ctr_mayor_edu="m_ft_col m_ft_stem m_ft_key m_colgnb m_col78 m_pt1 m_pt2"

// TABLE 3: Summary Statistics Table: Secretaries and Mayors by CityxYear Obs (Columns (1) & (2))
// Summary Statistics by CityxOfficial Obs (Columns (3) & (4)) are obtained using "Sec_and_Mayor_Characteristics_indivcity.do"
tabstat $ctr_sec_demo $ctr_sec_edu $ctr_sec_poly, statistics(mean sd count) save
return list
matrix PSStats=r(StatTotal)'
putexcel set SumStats_Sec, replace
putexcel A2=matrix(PSStats),rownames
tabstat $ctr_mayor_demo $ctr_mayor_edu $ctr_mayor_poly, statistics(mean sd count) save
return list
matrix MayorStats=r(StatTotal)'
putexcel set SumStats_Mayor, replace
putexcel A2=matrix(MayorStats),rownames


// exclude befyearsinpr and m_befyearsinpr from LASSO since they have very little variation
global ctr_sec_poly="yearsinpos yearstore match_pr befenterprise befresearch befsecretary befctymayor befctysec befcitymayor befcitysec befcentralgov befyearsincity befotherprov"
global ctr_mayor_poly="m_yearsinpos m_yearstore m_match_pr m_befenterprise m_befresearch m_befsecretary m_befctymayor m_befctysec m_befcitymayor m_befcitysec m_befcentralgov m_befyearsincity m_befotherprov"


// creating factor variables for province and year for baseline LASSO specification
gen dprovince=posprovince
qui tab dprovince, gen(dprovince)
drop dprovince
*global ctr_prov="province2 province3 province5 province6 province8 province9 province10 province11 province12 province13 province14 province15 province16 province17 province18 province19 province20 province21 province22 province23 province24 province25 province26 province27 province29 province30"
global ctr_province="dprovince2 dprovince3 dprovince5 dprovince6 dprovince8 dprovince9 dprovince10 dprovince11 dprovince12 dprovince13 dprovince14 dprovince15 dprovince16 dprovince17 dprovince18 dprovince19 dprovince20 dprovince21 dprovince22 dprovince23 dprovince24 dprovince25 dprovince26 dprovince27 dprovince29 dprovince30"

qui tab year, gen(time)

qui tab id, gen (cid)
global dumsgroupfe="posrk2 posrk3 posrk4 dprovince* time*"
global dumsfe="time* cid*"

///// BASELINE LASSO RESULTS
lassoShooting propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1)
global Selfe1 `r(selected)'
lassoShooting manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1)
global Selfe2 `r(selected)'

local Selfe1 $Selfe1
local Selfe2 $Selfe2
local Selfe : list Selfe1 | Selfe2
global Selfe `Selfe'


//// Panel B of Table 4 
eststo clear
* baseline post-lasso propFx LASSO
eststo:  xtreg propFx $Selfe posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
* city fixed effects propFx LASSO
eststo:  xtreg propFx $Selfe i.year, fe cluster(id)

*baseline post-lasso manipdays LASSO
eststo: xtreg manipdays $Selfe posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
*city fixed effects manipdays LASSO
eststo: xtreg manipdays $Selfe i.year, fe cluster(id)
esttab using postlasso_baseline.tex, replace b(3) noconstant drop(posrk2 posrk3 posrk4 *.year dprovince*)
eststo clear



///// Panel C of Table 4
eststo clear
tabstat propFx manipdays totday, statistics(mean sd min max count) save
return list
matrix maniptotdaystats=r(StatTotal)
putexcel set manip_totday_stats, replace
putexcel A2=matrix(maniptotdaystats),rownames
eststo clear

hist propFx
hist manipdays



////// Table 5: Robustness Check I of Key College Variable for Party Secretary (LASSO without Key College)
global ctr_sec_edunokey="ft_col ft_stem colgnb col78 pt1 pt2"
*Group FE LASSO 1: no interactions & witout PS key college variable

lassoShooting propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edunokey ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1)
global Selfer11 `r(selected)'
lassoShooting manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edunokey ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1)
global Selfer12 `r(selected)'

local Selfer11 $Selfer11
local Selfer12 $Selfer12
local Selfer1: list Selfer11 | Selfer12
global Selfer1 `Selfer1'

eststo: xtreg propFx $Selfer1 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg propFx $Selfer1 i.year, fe cluster(id)
eststo: xtreg manipdays $Selfer1 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg manipdays $Selfer1 i.year, fe cluster(id)
esttab using postlasso_robust1_nokey.tex, replace  b(3) noconstant drop(*.year posrk2 posrk3 posrk4 dprovince*)
eststo clear

////  Table 6: Robustness Check II of Key College Variable for Party Secretary (Varying Control Variables) 
global dumsgroupfer21="dprovince* time*"
lassoShooting propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfer21) lasiter(1000) verbose(0) fdisplay(1)
global Selfer211 `r(selected)'
lassoShooting manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfer21) lasiter(1000) verbose(0) fdisplay(1)
global Selfer212 `r(selected)'

local Selfer211 $Selfer211
local Selfer212 $Selfer212
local Selfer21: list Selfer211 | Selfer212
global Selfer21 `Selfer21'

global dumsgroupfer22="dprovince*"
lassoShooting propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfer22) lasiter(1000) verbose(0) fdisplay(1)
global Selfer221 `r(selected)'
lassoShooting manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfer22) lasiter(1000) verbose(0) fdisplay(1)
global Selfer222 `r(selected)'

local Selfer221 $Selfer221
local Selfer222 $Selfer222
local Selfer22: list Selfer221 | Selfer222
global Selfer22 `Selfer22'

// Panel A of Table 6
eststo: xtreg propFx $Selfer21 dprovince* i.year, re cluster(id)
eststo: xtreg propFx $Selfer21 i.year, fe cluster(id)
eststo: xtreg propFx $Selfer22 dprovince*, re cluster(id)
eststo: xtreg propFx $Selfer22 i.year, fe cluster(id)
esttab using postlasso_robust2_propFx.tex, replace  b(3) noconstant drop(*.year dprovince*)
eststo clear

// Panel B of Table 6
eststo: xtreg manipdays $Selfer21 dprovince* i.year, re cluster(id)
eststo: xtreg manipdays $Selfer21 i.year, fe cluster(id)
eststo: xtreg manipdays $Selfer22 dprovince*, re cluster(id)
eststo: xtreg manipdays $Selfer22 i.year, fe cluster(id)

esttab using postlasso_robust2_manipdays.tex, replace  b(2) noconstant drop(*.year dprovince*)
eststo clear

/////// Tables 7&8: LASSO Robustness Check III: Relaxing Penalty Term*****************************
//Penalty=250, 200, 150, 100
//lambda(250)
lassoShooting propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1) lambda(250)
global Selfer311 `r(selected)'
lassoShooting manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1) lambda(250)
global Selfer312 `r(selected)'

local Selfer311 $Selfer311
local Selfer312 $Selfer312
local Selfer31 : list Selfer311 | Selfer312
global Selfer31 `Selfer31'

//lambda(200)
lassoShooting propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1) lambda(200)
global Selfer321 `r(selected)'
lassoShooting manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1) lambda(200)
global Selfer322 `r(selected)'

local Selfer321 $Selfer321
local Selfer322 $Selfer322
local Selfer32 : list Selfer321 | Selfer322
global Selfer32 `Selfer32'

//lambda(150)
lassoShooting propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1) lambda(150)
global Selfer331 `r(selected)'
lassoShooting manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1) lambda(150)
global Selfer332 `r(selected)'

local Selfer331 $Selfer331
local Selfer332 $Selfer332
local Selfer33 : list Selfer331 | Selfer332
global Selfer33 `Selfer33'

//lambda(100)
lassoShooting propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1) lambda(100)
global Selfer341 `r(selected)'
lassoShooting manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1) lambda(100)
global Selfer342 `r(selected)'

local Selfer341 $Selfer341
local Selfer342 $Selfer342
local Selfer34 : list Selfer341 | Selfer342
global Selfer34 `Selfer34'


/// Table 7
eststo: xtreg propFx $Selfer31 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg propFx $Selfer31 i.year, fe cluster(id)
eststo: xtreg propFx $Selfer32 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg propFx $Selfer32 i.year, fe cluster(id)

eststo: xtreg propFx $Selfer33 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg propFx $Selfer33 i.year, fe cluster(id)

eststo: xtreg propFx $Selfer34 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg propFx $Selfer34 i.year, fe cluster(id)
esttab using postlasso_robust3_propFx.tex, replace b(3) noconstant drop(*.year posrk2 posrk3 posrk4 dprovince*)
eststo clear

///Table 8
eststo: xtreg manipdays $Selfer31 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg manipdays $Selfer31 i.year, fe cluster(id)
eststo: xtreg manipdays $Selfer32 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg manipdays $Selfer32 i.year, fe cluster(id)
eststo: xtreg manipdays $Selfer33 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg manipdays $Selfer33 i.year, fe cluster(id)

eststo: xtreg manipdays $Selfer34 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg manipdays $Selfer34 i.year, fe cluster(id)
esttab using postlasso_robust3_manipdays.tex, replace  b(2) noconstant drop(*.year posrk2 posrk3 posrk4 dprovince*)
eststo clear
log close


log using "log_PSEC_analysis.log", replace

/// Table 9
// Panel A of Table 9: Observable Promotion Characteristics
global promvars="aftpromoted aftprovleaderpg aftprovleadernc aftprovdeptchief aftcentgov"

local npp : word count $promvars
matrix seckeypromdiff = J(`npp',3,0)
matrix rownames seckeypromdiff=$promvars
matrix colnames seckeypromdiff="ft_key diff tstat"
forvalues ii = 1/`npp' {
local temp: word `ii' of $promvars
reg `temp' ft_key, cluster(id)
matrix coeff=e(b)
matrix seckeypromdiff[`ii',1]=coeff[1,2]+coeff[1,1]
matrix seckeypromdiff[`ii',2]=coeff[1,1]
matrix Var=e(V)
matrix seckeypromdiff[`ii',3]=coeff[1,1]/sqrt(Var[1,1])
}
putexcel set Prom_Stats_Secretary_ttest, replace
putexcel A2=matrix(seckeypromdiff), rownames

// Column I of Panel B of Table 9: Correlation between Promotion and Manipulation

xtreg propFx posrk2 posrk3 posrk4 if ft_key==1, re
predict pFxposreskey, e
xtreg propFx posrk2 posrk3 posrk4 if ft_key==0, re
predict pFxposresnkey, e
local nprom: word count $promvars
forvalues pp = 1/`nprom' {
local temp: word `pp' of $promvars
xtreg `temp' posrk2 posrk3 posrk4 if ft_key==1, re cluster(id)
predict `temp'posreskey, e
xtreg `temp' posrk2 posrk3 posrk4 if ft_key==0, re cluster(id)
predict `temp'posresnkey, e
}
correlate pFxposreskey aft*posreskey
return list
putexcel set Manip_Promote_Correlation_key_pos, replace
matrix mpromocorrkeypos=r(C)
matrix Ncorrkeypos=r(N)
putexcel A2=matrix(mpromocorrkeypos), rownames nformat(number_d2)
putexcel A14=matrix(Ncorrkeypos)

correlate pFxposresnkey aft*posresnkey
return list
putexcel set Manip_Promote_Correlation_nkey_pos, replace
matrix mpromocorrnkeypos=r(C)
matrix Ncorrnkeypos=r(N)
putexcel A2=matrix(mpromocorrnkeypos), rownames nformat(number_d2)
putexcel A14=matrix(Ncorrnkeypos)

// Column II of Panel B of Table 9: Correlation between Promotion and Manipulation (City FE)
qui xtreg propFx if ft_key==1, fe cluster(id)
predict propFxfekey, e
qui xtreg propFx if ft_key==0, fe cluster(id)
predict propFxfenkey, e

local nprom: word count $promvars
forvalues pp = 1/`nprom' {
local temp: word `pp' of $promvars
xtreg `temp' if ft_key==1, fe cluster(id)
predict `temp'fereskey, e
xtreg `temp' if ft_key==0, fe cluster(id)
predict `temp'feresnkey, e
}
correlate propFxfekey aft*fereskey
return list
putexcel set Manip_Promote_Correlation_key, replace
matrix mpromocorrkey=r(C)
matrix Ncorrkey=r(N)
putexcel A2=matrix(mpromocorrkey), rownames nformat(number_d2)
putexcel A14=matrix(Ncorrkey)

correlate propFxfenkey aft*feresnkey
return list
putexcel set Manip_Promote_Correlation_nkey, replace
matrix mpromocorrnkey=r(C)
matrix Ncorrnkey=r(N)
putexcel A2=matrix(mpromocorrnkey), rownames nformat(number_d2)
putexcel A14=matrix(Ncorrnkey)

// Column III of Panel B of Table 9: Correlation between Promotion and Manipulation (City & Year FE)
qui xtreg propFx i.year if ft_key==1, fe cluster(id)
predict pFxreskey, e
qui xtreg propFx i.year if ft_key==0, fe cluster(id)
predict pFxresnkey, e

local nprom: word count $promvars
forvalues pp = 1/`nprom' {
local temp: word `pp' of $promvars
xtreg `temp' i.year if ft_key==1, fe cluster(id)
predict `temp'yfereskey, e
xtreg `temp' i.year if ft_key==0, fe cluster(id)
predict `temp'yferesnkey, e
}
correlate pFxreskey aft*yfereskey
return list
putexcel set Manip_Promote_Correlation_key_yfe, replace
matrix mpromocorrkeyyfe=r(C)
matrix Ncorrkeyyfe=r(N)
putexcel A2=matrix(mpromocorrkeyyfe), rownames nformat(number_d2)
putexcel A14=matrix(Ncorrkeyyfe)

correlate pFxresnkey aft*yferesnkey
return list
putexcel set Manip_Promote_Correlation_nkey_yfe, replace
matrix mpromocorrnkeyyfe=r(C)
matrix Ncorrnkeyyfe=r(N)
putexcel A2=matrix(mpromocorrnkeyyfe), rownames nformat(number_d2)
putexcel A14=matrix(Ncorrnkeyyfe)


////// TABLE 10: GDP, PSEC and Manipulation
// Panel A: Observable Economic Differences: Overall GDP & GDP by Sector
global econvars="var34 var35 var36 var37 "

local nee : word count $econvars
matrix seckeyecondiff = J(`nee',3,0)
matrix rownames seckeyecondiff=$econvars
matrix colnames seckeyecondiff="ft_key diff tstat"
forvalues ii = 1/`nee' {
local temp: word `ii' of $econvars
reg `temp' ft_key, cluster(id)
matrix coeff=e(b)
matrix seckeyecondiff[`ii',1]=coeff[1,2]+coeff[1,1]
matrix seckeyecondiff[`ii',2]=coeff[1,1]
matrix Var=e(V)
matrix seckeyecondiff[`ii',3]=coeff[1,1]/sqrt(Var[1,1])
}
putexcel set Econ_Stats_Secretary_ttest, replace
putexcel A2=matrix(seckeyecondiff), rownames


// Panel B: Conditional Correlation between Manipulation and Economic Variables
qui xtreg var34 i.year if ft_key==1, fe cluster(id)
predict v34reskey, e
qui xtreg var34 i.year if ft_key==0, fe cluster(id)
predict v34resnkey, e
qui xtreg var35 i.year if ft_key==1, fe cluster(id)
predict v35reskey, e
qui xtreg var35 i.year if ft_key==0, fe cluster(id)
predict v35resnkey, e
qui xtreg var36 i.year if ft_key==1, fe cluster(id)
predict v36reskey, e
qui xtreg var36 i.year if ft_key==0, fe cluster(id)
predict v36resnkey, e
qui xtreg var37 i.year if ft_key==1, fe cluster(id)
predict v37reskey, e
qui xtreg var37 i.year if ft_key==0, fe cluster(id)
predict v37resnkey, e

corr pFxreskey v34reskey v35reskey v36reskey v37reskey
return list
putexcel set Manip_Econ_Correlation_key, replace
matrix meconcorrkey=r(C)
matrix meconcorrn=r(N)
putexcel A2=matrix(meconcorrkey), rownames nformat(number_d2)
putexcel A10=matrix(meconcorrn), rownames  nformat(number_d2)
corr pFxresnkey v34resnkey v35resnkey v36resnkey v37resnkey
return list
putexcel set Manip_Econ_Correlation_nkey, replace
matrix meconcorrnkey=r(C)
matrix meconcorrnkeyn=r(N)
putexcel A2=matrix(meconcorrnkey), rownames nformat(number_d2)
putexcel A10=matrix(meconcorrnkeyn), rownames  nformat(number_d2)

log close



///// Online Appendix: Supplementary LASSO and Elastic Net RESULTS
log using "log_LASSO_SA_checks.log", replace

///  Online Appendix Table A2: Comparison of SD of Original Variables and FE Residualized Characteristics
local ctr_sec_demo $ctr_sec_demo
local ctr_sec_edu $ctr_sec_edu
local ctr_sec_poly $ctr_sec_poly

local ctr_characteristics : list ctr_sec_demo | ctr_sec_edu
local ctr_characteristics: list ctr_characteristics | ctr_sec_poly 
global ctr_characteristics `ctr_characteristics'
local nchar: word count $ctr_characteristics
forvalues pp = 1/`nchar' {
local temp: word `pp' of $ctr_characteristics
xtreg `temp' i.year, fe cluster(id)
predict `temp'feres, e
}

tabstat $ctr_characteristics, statistics(mean sd count) save
return list
matrix PSStats=r(StatTotal)'
putexcel set Sec_Char_sd, replace
putexcel A2=matrix(PSStats),rownames


tabstat *feres, statistics(mean sd count) save
return list
matrix PSStatsres=r(StatTotal)'
putexcel set Sec_Char_feres_sd, replace
putexcel A2=matrix(PSStatsres),rownames


local ctr_mayor_demo $ctr_mayor_demo
local ctr_mayor_edu $ctr_mayor_edu
local ctr_mayor_poly $ctr_mayor_poly

local ctr_m_characteristics : list ctr_mayor_demo | ctr_mayor_edu
local ctr_m_characteristics: list ctr_m_characteristics | ctr_mayor_poly 
global ctr_m_characteristics `ctr_m_characteristics'
local nchar: word count $ctr_m_characteristics

tabstat $ctr_m_characteristics, statistics(mean sd count) save
return list
matrix MStats=r(StatTotal)'
putexcel set M_Char_sd, replace
putexcel A2=matrix(MStats),rownames


forvalues pp = 1/`nchar' {
local temp: word `pp' of $ctr_m_characteristics
xtreg `temp' i.year, fe cluster(id)
predict `temp'_m_feres, e
}

tabstat *_m_feres, statistics(mean sd count) save
return list
matrix MStatsres=r(StatTotal)'
putexcel set M_Char_res_sd, replace
putexcel A2=matrix(MStatsres),rownames

//// Online Appendix Tables A4-A7:  City and Year FE LASSO and Elastic Net with Penalty Level minimizing Information Criteria 
/// This section of the code requires the package lassopack
/// Command lasso2 with alpha(1) yields lasso, with alpha(0.25/0.5/0.75) yields elastic net
eststo clear
matrix AICmat=J(4,2,.)
matrix LAICmat=J(4,2,.)
matrix AICCmat=J(4,2,.)
matrix LAICCmat=J(4,2,.)
matrix BICmat=J(4,2,.)
matrix LBICmat=J(4,2,.)
matrix EBICmat=J(4,2,.)
matrix LEBICmat=J(4,2,.)
scalar c=0
forvalues i=0.25 (0.25) 1 {
scalar c=c+1
lasso2 propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(`i') partial(time*) fe
ereturn list

matrix AICmat[c,1]=`e(aicmin)'
matrix LAICmat[c,1]=`e(laic)'
matrix AICCmat[c,1]=`e(aiccmin)'
matrix LAICCmat[c,1]=`e(laicc)'
matrix BICmat[c,1]=`e(bicmin)'
matrix LBICmat[c,1]=`e(lbic)'
matrix EBICmat[c,1]=`e(ebicmin)'
matrix LEBICmat[c,1]=`e(lebic)'
lasso2 manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(`i') partial(time*) fe
ereturn list
matrix AICmat[c,2]=`e(aicmin)'
matrix LAICmat[c,2]=`e(laic)'
matrix AICCmat[c,2]=`e(aiccmin)'
matrix LAICCmat[c,2]=`e(laicc)'
matrix BICmat[c,2]=`e(bicmin)'
matrix LBICmat[c,2]=`e(lbic)'
matrix EBICmat[c,2]=`e(ebicmin)'
matrix LEBICmat[c,2]=`e(lebic)'

//ebic
lasso2 propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(`i') partial(time*) fe ic(ebic) lic(ebic) postresults
ereturn list
global l2ebselp `e(selected)'
lasso2 manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(`i') partial(time*) fe ic(ebic)  lic(ebic) postresults
ereturn list
global l2ebselm `e(selected)'

local l2ebselp $l2ebselp
local l2ebselm $l2ebselm
local l2ebsel : list l2ebselp | l2ebselm
global l2ebsel `l2ebsel'

//bic
lasso2 propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(1) partial(time*) fe ic(bic) lic(bic) postresults
ereturn list
global l2bselp `e(selected)'

lasso2 manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(`i') partial(time*) fe ic(bic)  lic(bic) postresults
ereturn list
global l2bselm `e(selected)'

local l2bselp $l2bselp
local l2bselm $l2bselm
local l2bsel : list l2bselp | l2bselm
global l2bsel `l2bsel'

//aicc
lasso2 propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(`i') partial(time*) fe ic(aicc) lic(aicc) postresults
ereturn list
global l2acselp `e(selected)'

lasso2 manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(`i') partial(time*) fe ic(aicc)  lic(aicc) postresults
ereturn list
global l2acselm `e(selected)'

local l2acselp $l2acselp
local l2acselm $l2acselm
local l2acsel : list l2acselp | l2acselm
global l2acsel `l2acsel'

//aic
lasso2 propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(`i') partial(time*) fe ic(aic) lic(aic) postresults
ereturn list
global l2aselp `e(selected)'

lasso2 manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(`i') partial(time*) fe ic(aic)  lic(aic) postresults
ereturn list
global l2aselm `e(selected)'

local l2aselp $l2aselp
local l2aselm $l2aselm
local l2asel : list l2aselp | l2aselm
global l2asel `l2asel'

eststo clear
eststo: xtreg propFx $l2ebsel i.year, fe cluster(id)
eststo: xtreg propFx $l2bsel i.year, fe cluster(id)
eststo: xtreg propFx $l2acsel i.year, fe cluster(id)
eststo: xtreg propFx $l2asel i.year, fe cluster(id)
eststo: xtreg manipdays $l2ebsel i.year, fe cluster(id)
eststo: xtreg manipdays $l2bsel i.year, fe cluster(id)
eststo: xtreg manipdays $l2acsel i.year, fe cluster(id)
eststo: xtreg manipdays $l2asel i.year, fe cluster(id)


esttab using "lasso2_alpha_`i'_ic_fe.tex", replace  b(3) noconstant drop(*.year)
}

matrix ICmat=AICmat,AICCmat,BICmat,EBICmat
putexcel set IC_matrix_elasticnet, replace
putexcel A2=matrix(ICmat)

///Online Appendix Table A8: Baseline LASSO Control Variables (Province, City-rank and Year FE) with Penalty minimizing Information Criteria
// ebic
lasso2 propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu $dumsgroupfe,  alpha(1) partial($dumsgroupfe) ic(ebic) lic(ebic) postresults
ereturn list
global l2gebselp `e(selected)'

lasso2 manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu $dumsgroupfe,  alpha(1) partial($dumsgroupfe) ic(ebic)  lic(ebic) postresults
ereturn list
global l2gebselm `e(selected)'

local l2gebselp $l2gebselp
local l2gebselm $l2gebselm
local l2gebsel : list l2gebselp | l2gebselm
global l2gebsel `l2gebsel'


//bic
lasso2 propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu $dumsgroupfe,  alpha(1) partial($dumsgroupfe) ic(bic) lic(bic) postresults
ereturn list
global l2gbselp `e(selected)'

lasso2 manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu $dumsgroupfe,  alpha(1) partial($dumsgroupfe) ic(bic)  lic(bic) postresults
ereturn list
global l2gbselm `e(selected)'

local l2gbselp $l2gbselp
local l2gbselm $l2gbselm
local l2gbsel : list l2gbselp | l2gbselm
global l2gbsel `l2gbsel'


//aicc
lasso2 propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu $dumsgroupfe,  alpha(1) partial($dumsgroupfe) ic(aicc) lic(aicc) postresults
ereturn list
global l2gacselp `e(selected)'

lasso2 manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu $dumsgroupfe,  alpha(1) partial($dumsgroupfe) ic(aicc)  lic(aicc) postresults
ereturn list
global l2gacselm `e(selected)'

local l2gacselp $l2gacselp
local l2gacselm $l2gacselm
local l2gacsel : list l2gacselp | l2gacselm
global l2gacsel `l2gacsel'


//aic
lasso2 propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu $dumsgroupfe,  alpha(1) partial($dumsgroupfe) ic(aic) lic(aic) postresults
ereturn list
global l2gaselp `e(selected)'

lasso2 manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu $dumsgroupfe,  alpha(1) partial($dumsgroupfe) ic(aic)  lic(aic) postresults
ereturn list
global l2gaselm `e(selected)'

local l2gaselp $l2gaselp
local l2gaselm $l2gaselm
local l2gasel : list l2gaselp | l2gaselm
global l2gasel `l2gasel'

eststo clear
eststo: xtreg propFx $l2gebsel i.year, fe cluster(id)
eststo: xtreg propFx $l2gbsel i.year, fe cluster(id)
eststo: xtreg propFx $l2gacsel i.year, fe cluster(id)
eststo: xtreg propFx $l2gasel i.year, fe cluster(id)

eststo: xtreg manipdays $l2gebsel i.year, fe cluster(id)
eststo: xtreg manipdays $l2gbsel i.year, fe cluster(id)
eststo: xtreg manipdays $l2gacsel i.year, fe cluster(id)
eststo: xtreg manipdays $l2gasel i.year, fe cluster(id)
esttab using "lasso_gfe_ic_results.tex", replace b(3) noconstant drop(*.year)

eststo clear

log close



